package com.wyg.epdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyg.epdata.entity.Article;
import com.wyg.epdata.entity.ArticleEntity;
import com.wyg.epdata.mapper.ArticleEntityMapper;
import com.wyg.epdata.service.ArticleEntityService;
import com.wyg.epdata.service.CompanyNumService;
import com.wyg.epcommonbase.common.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WangYongGan
 * @since 2021-11-11
 */
@Component
public class ArticleEntityServiceImpl extends ServiceImpl<ArticleEntityMapper, ArticleEntity> implements ArticleEntityService {
    @Autowired
    CompanyNumService companyNumService;

    @Override
    public void saveArticleEntity(Article article, String entity, boolean isReg) {
        ArticleEntity articleEntity = new ArticleEntity();
        articleEntity.setEntity(entity);
        articleEntity.setArticleId(article.getId());
        articleEntity.setReg(isReg);
        baseMapper.insert(articleEntity);
    }

    @Override
    public List<String> getArticleByEntity(String name) {
        List<String> articleIds = baseMapper.selectList(new QueryWrapper<ArticleEntity>().eq("entity", name)).stream().map(o -> o.getArticleId()).collect(Collectors.toList());
        return articleIds;
    }

    @Override
    public List<String> getIdsByEntity(String entity, int reg) {
        AssertUtil.isTrue(!companyNumService.exists(entity), 112, "企业不存在");
        QueryWrapper<ArticleEntity> wrapper = new QueryWrapper<>();
        wrapper.ne("is_reg", 1 - reg);
        List<ArticleEntity> articleEntities = baseMapper.selectList(wrapper.lambda().eq(ArticleEntity::getEntity, entity));
        articleEntities = Optional.ofNullable(articleEntities).orElse(new ArrayList<>());
        return articleEntities.stream().map(ArticleEntity::getArticleId).collect(Collectors.toList());
    }

}
